﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WoChu.Autorun.Base.Dao;

namespace MvcAppWeb.Controllers
{
    public class GoodsInfoManageController : BaseDataController
    {

        public ActionResult GoodsInfoManage()
        {
            //if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            //else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }

        ///<summary>
        ///查询预估订单信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string goodsCode, string goodsName, string lookup, string selectIsShow,string QuRuleClass)
        {
            string where = "  where 1=1 and a.status='1' ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (goodsCode != "")
            {
                where += " and goodsCode='" + goodsCode + "'";
            }
            else if (goodsName != "")
            {
                where += " and goodsName like '%" + goodsName + "%'";
            }
            else if (lookup != "" && goodsCode == "")
            {
                where += " and CELLNO = '" + lookup + "'";
            }
            if (selectIsShow != "" && goodsCode == "")
            {
                where += " and ISSHOW = '" + selectIsShow + "'";
            }
            if (QuRuleClass != "")
            {
                where += " and RULECLASS='" + QuRuleClass + "'";
            }
            var retData = new object();
            BGoodsCellNo db = new BGoodsCellNo();
            retData = db.GetGoodsInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
            
        }


        ///<summary>
        ///查询箱查询商品库位码
        ///</summary>
        ///<returns></returns>
        public JsonResult GetGoodsLookUpInfo()
        {
            BDeliveryBox db = new BDeliveryBox();
            var retData = new object();
            List<LookItem> lst = db.GetDeliveryBoxType("CELLNO");//获取规则
            lst.Insert(0,new LookItem() { FIELDVALUE = "", LOOKUP = "全部" });
            retData = lst.ToList();  
            return Json(retData); 
        }

        ///<summary>
        ///查询箱查询商品自定义库位
        ///</summary>
        ///<returns></returns>
        public JsonResult GetGoodsCustomLookUpInfo()
        {
            BDeliveryBox db = new BDeliveryBox();
            var retData = new object();
            retData = db.GetDeliveryBoxType("CUSTOMCELLNO").ToList();//获取规则
            return Json(retData);
        }

        ///<summary>
        ///查询预估订单信息
        ///</summary>
        ///<returns></returns>
        public JsonResult GetGoodsDetail(string goodsCode)
        {

            BGoodsCellNo db = new BGoodsCellNo(); 
            var result = db.GetGoodsDetail(goodsCode).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);
        }



        [HttpPost]
        /// <summary>
        /// 审核预购订单
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string SaveData()
        {
            string goodsCode = Request["hiddenGoodsCode"];
            string customCellNos = Request["CustomCELLNO"];
            int pkgLossQty = Convert.ToInt32(Request["txtGoodsJhl"]);
            decimal saleStorkQTY = Convert.ToDecimal(Request["txtSaleStorkQTY"]);
            string isShow = Request["selectIsShow"].ToString();
            string ruleClass = Request["txtRuleClass"].ToString();

            string sjson = string.Empty;
            if (goodsCode != null && goodsCode != "")
            {
                bool b = false;
                List<Goods> list = new List<Goods>();
                BGoodsCellNo db = new BGoodsCellNo();

                Goods goods = new Goods();
                goods.GOODSCODE = goodsCode;
                goods.CUSTOMCELLNO = customCellNos;
                goods.PKGLOSSQTY = Convert.ToDecimal(pkgLossQty);
                goods.SALESTOCKQTY = Convert.ToDecimal(saleStorkQTY);
                goods.ISSHOW = isShow;
                goods.RULECLASS = ruleClass;
                int n = db.UpdateGoodsCellNo(goods);
                if (n > 0)
                {
                    b = true;
                }
                else
                {
                    b = false;
                }

                if (b)
                {
                    sjson = "{success:true,msg:'修改成功'}";
                }
                else
                {
                    sjson = "{success:true,msg:'修改失败'}";
                }
            }
            else
            {
                sjson = "{success:false,msg:'请选择数据后再批量修改'}";
            }

            return sjson;
        }



        [HttpPost]
        /// <summary>
        /// 审核预购订单
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string SaveDataBatch()
        {
            string goodsCodes = Request["hiddenGoodsCodeBatch"];
            string customCellNos = Request["CustomCELLNO_batch"];
            string sjson = string.Empty;
            if (goodsCodes != null && goodsCodes != "")
            {

                bool b = false;
                List<Goods> list = new List<Goods>();
                BGoodsCellNo db = new BGoodsCellNo();
                if (goodsCodes.IndexOf(',') > -1)
                {
                    goodsCodes = goodsCodes.Substring(0, goodsCodes.Length - 1);
                    foreach (var code in goodsCodes.Split(','))
                    {
                        Goods goods = new Goods();
                        goods.GOODSCODE = code;
                        goods.CUSTOMCELLNO = customCellNos;
                        list.Add(goods);
                    }
                    b = db.UpdateGoodsCellNoBatch(list);
                }

                if (b)
                {
                    sjson = "{success:true,msg:'修改成功'}";
                }
                else
                {
                    sjson = "{success:true,msg:'修改失败'}";
                }
            }
            else
            {
                sjson = "{success:false,msg:'请选择数据后再批量修改'}";
            }

            return sjson;
        }

    }
}
